﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WXYW.Entities;
using WXYW.DAL;
using WXYW.Model;
using System.Globalization;

namespace WXYW.BLL
{
    public class QWXZQService : RepositoryBase
    {
        #region 概要信息
        /// <summary>
        /// 维修周期 是指：客户收货的日期-我们收货日期-报价周期-1
        /// </summary>

        public QWXZQModel GetSummaryModel(int year, int month)
        {
            QWXZQModel model = new QWXZQModel();

            model.ZTOfHalfYear = GetZTOfHalfYear(year, month).ToString();
            model.ZTOfMonth = GetZTOfMonth(year, month).ToString();
            model.ZTOfSeason = GetZTOfSeason(year, month).ToString();
            model.ZTOfYear = GetZTOfYear(year, month).ToString();

            model.ApplyCountOfHalfYear = GetApplyCountOfHalfYear(year, month).ToString();
            model.ApplyCountOfMonth = GetApplyCountOfMonth(year, month).ToString();
            model.ApplyCountOfSeason = GetApplyCountOfSeason(year, month).ToString();
            model.ApplyCountOfYear = GetApplyCountOfYear(year, month).ToString();

            model.NotCompleteCountOfHalfYear = GetNotCompleteCountOfHalfYear(year, month).ToString();
            model.NotCompleteCountOfMonth = GetNotCompleteCountOfMonth(year, month).ToString();
            model.NotCompleteCountOfSeason = GetNotCompleteCountOfSeason(year, month).ToString();
            model.NotCompleteCountOfYear = GetNotCompleteCountOfYear(year, month).ToString();

            model.CompleteCountOfHalfYear = GetCompleteCountOfHalfYear(year, month).ToString();
            model.CompleteCountOfMonth = GetCompleteCountOfMonth(year, month).ToString();
            model.CompleteCountOfSeason = GetCompleteCountOfSeason(year, month).ToString();
            model.CompleteCountOfYear = GetCompleteCountOfYear(year, month).ToString();

            model.L15OfHalfYear = GetL15CountOfHalfYear(year, month).ToString();
            model.L15OfMonth = GetL15CountOfMonth(year, month).ToString();
            model.L15OfSeason = GetL15CountOfSeason(year, month).ToString();
            model.L15OfYear = GetL15CountOfYear(year, month).ToString();

            model.S7OfHalfYear = GetS7CountOfHalfYear(year, month).ToString();
            model.S7OfMonth = GetS7CountOfMonth(year, month).ToString();
            model.S7OfSeason = GetS7CountOfSeason(year, month).ToString();
            model.S7OfYear = GetS7CountOfYear(year, month).ToString();

            model.S7To15OfHalfYear = Get7To15CountOfHalfYear(year, month).ToString();
            model.S7To15OfMonth = Get7To15CountOfMonth(year, month).ToString();
            model.S7To15OfSeason = Get7To15CountOfSeason(year, month).ToString();
            model.S7To15OfYear = Get7To15CountOfYear(year, month).ToString();
            
            return model;
        }

        public int GetZTOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p=>p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int totalQWXZQ = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = (application.ApplicationForm.ClientReceiveDateTime.Value - application.ApplicationForm.ReceiveDateTime.Value).Days;
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = (application.ConfirmQuotationDateTime.Value - application.ApplicationForm.QuatationDatetime.Value).Days + 1;
                }
                totalQWXZQ += timeSpan - quoteTimeSpan - 1;
            }

            if (totalQWXZQ > 0)
            {
                return totalQWXZQ / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetZTOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value > endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int totalQWXZQ = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = (application.ApplicationForm.ClientReceiveDateTime.Value - application.ApplicationForm.ReceiveDateTime.Value).Days;
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = (application.ConfirmQuotationDateTime.Value - application.ApplicationForm.QuatationDatetime.Value).Days + 1;
                }
                totalQWXZQ += timeSpan - quoteTimeSpan - 1;
            }

            if (totalQWXZQ > 0)
            {
                return totalQWXZQ / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetZTOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate && p.ApplicationForm.ReceiveDateTime.Value > startDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int totalQWXZQ = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = (application.ApplicationForm.ClientReceiveDateTime.Value - application.ApplicationForm.ReceiveDateTime.Value).Days;
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = (application.ConfirmQuotationDateTime.Value - application.ApplicationForm.QuatationDatetime.Value).Days + 1;
                }
                totalQWXZQ += timeSpan - quoteTimeSpan - 1;
            }

            if (totalQWXZQ > 0)
            {
                return totalQWXZQ / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetZTOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int totalQWXZQ = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = (application.ApplicationForm.ClientReceiveDateTime.Value - application.ApplicationForm.ReceiveDateTime.Value).Days;
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = (application.ConfirmQuotationDateTime.Value - application.ApplicationForm.QuatationDatetime.Value).Days + 1;
                }
                totalQWXZQ += timeSpan - quoteTimeSpan - 1;
            }

            if (totalQWXZQ > 0)
            {
                return totalQWXZQ / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetApplyCountOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year-1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate);
            return applications.Count();
        }

        public int GetApplyCountOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value > endDate);

            return applications.Count();
        }

        public int GetApplyCountOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate && p.ApplicationForm.ReceiveDateTime.Value > startDate);

            return applications.Count();
        }

        public int GetApplyCountOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate);
            return applications.Count();
        }
        #endregion

        #region 未完成数量
        public int GetNotCompleteCountOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                !p.RepairCompleteDateTime.HasValue);
            return applications.Count();
        }

        public int GetNotCompleteCountOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value > endDate &&
                !p.RepairCompleteDateTime.HasValue);

            return applications.Count();
        }

        public int GetNotCompleteCountOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate && p.ApplicationForm.ReceiveDateTime.Value > startDate &&
                !p.RepairCompleteDateTime.HasValue);

            return applications.Count();
        }

        public int GetNotCompleteCountOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                !p.RepairCompleteDateTime.HasValue);
            return applications.Count();
        }
        #endregion
        
        #region 完修数量
        public int GetCompleteCountOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                      p.RepairCompleteDateTime.HasValue && p.RepairCompleteDateTime.Value > startDate && p.RepairCompleteDateTime.Value < endDate);
            return applications.Count();
        }

        public int GetCompleteCountOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                   p.RepairCompleteDateTime.HasValue && p.RepairCompleteDateTime.Value > startDate && p.RepairCompleteDateTime.Value < endDate);

            return applications.Count();
        }

        public int GetCompleteCountOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.RepairCompleteDateTime.HasValue && p.RepairCompleteDateTime.Value > startDate && p.RepairCompleteDateTime.Value < endDate);

            return applications.Count();
        }

        public int GetCompleteCountOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                 p.RepairCompleteDateTime.HasValue && p.RepairCompleteDateTime.Value > startDate && p.RepairCompleteDateTime.Value < endDate);
            return applications.Count();
        }
        #endregion

        #region 小于等于7
        public int GetS7CountOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan <= 7)
                {
                    count++;
                }
            }
            return count;
        }

        public int GetS7CountOfMonth(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue &&
                p.ApplicationForm.ServiceStation.ID == stationID);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (application.ApplicationForm.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                }
                else if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan <= 7)
                {
                    count++;
                }
            }
            return count;
        }

        public int GetS7CountOfMonthByPerson(long userID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue &&
                p.User.UserID == userID);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (application.ApplicationForm.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                }
                else if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan <= 7)
                {
                    count++;
                }
            }
            return count;
        }


        public int GetS7CountOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value > endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan <= 7)
                {
                    count++;
                }
            }

            return count;
        }

        public int GetS7CountOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate && p.ApplicationForm.ReceiveDateTime.Value > startDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan <= 7)
                {
                    count++;
                }
            }

            return count;
        }

        public int GetS7CountOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan <= 7)
                {
                    count++;
                }
            }

            return count;
        }
        #endregion

        #region 大于7小于等于15
        public int Get7To15CountOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 7 && timeSpan <= 15)
                {
                    count++;
                }
            }
            return count;
        }

        public int Get7To15CountOfMonth(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                (p.ApplicationForm.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) &&
                p.ApplicationForm.ServiceStation.ID == stationID);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (application.ApplicationForm.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                }
                else if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }

                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 7 && timeSpan <= 15)
                {
                    count++;
                }
            }
            return count;
        }

        public int Get7To15CountOfMonthByPerson(long userID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                (p.ApplicationForm.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) &&
                p.User.UserID == userID);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (application.ApplicationForm.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                }
                else if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }

                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 7 && timeSpan <= 15)
                {
                    count++;
                }
            }
            return count;
        }

        public int Get7To15CountOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value > endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 7 && timeSpan <= 15)
                {
                    count++;
                }
            }

            return count;
        }

        public int Get7To15CountOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate && p.ApplicationForm.ReceiveDateTime.Value > startDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 7 && timeSpan <= 15)
                {
                    count++;
                }
            }

            return count;
        }

        public int Get7To15CountOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 7 && timeSpan <= 15)
                {
                    count++;
                }
            }

            return count;
        }
        #endregion

        #region 大于15
        public int GetL15CountOfMonth(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 15)
                {
                    count++;
                }
            }
            return count;
        }

        public int GetL15CountOfMonth(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                (p.ApplicationForm.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) &&
                p.ApplicationForm.ServiceStation.ID == stationID);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (application.ApplicationForm.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                }
                else if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }

                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 15)
                {
                    count++;
                }
            }
            return count;
        }

        public int GetL15CountOfMonthByPerson(long userID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 26);
            }

            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                (p.ApplicationForm.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) &&
                p.User.UserID == userID);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (application.ApplicationForm.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                }
                else if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }

                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 15)
                {
                    count++;
                }
            }
            return count;
        }

        public int GetL15CountOfSeason(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 3)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 3, 25);
            }
            else if (month > 3 & month <= 6)
            {
                startDate = new DateTime(year, 3, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else if (month > 6 && month <= 9)
            {
                startDate = new DateTime(year, 6, 26);
                endDate = new DateTime(year, 9, 25);
            }
            else
            {
                startDate = new DateTime(year, 9, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value > endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 15)
                {
                    count++;
                }
            }

            return count;
        }

        public int GetL15CountOfHalfYear(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            DateTime endDate = DateTime.Now;
            if (month <= 6)
            {
                startDate = new DateTime(year - 1, 12, 26);
                endDate = new DateTime(year, 6, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 6, 26);
                endDate = new DateTime(year, 12, 25);
            }

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate && p.ApplicationForm.ReceiveDateTime.Value > startDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 15)
                {
                    count++;
                }
            }

            return count;
        }

        public int GetL15CountOfYear(int year, int month)
        {
            DateTime startDate = new DateTime(year - 1, 12, 26);
            DateTime endDate = new DateTime(year, 12, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ClientReceiveDateTime.HasValue);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                int quoteTimeSpan = 0;
                if (application.ApplicationForm.QuatationDatetime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.QuatationDatetime.Value, application.ConfirmQuotationDateTime.Value);
                }
                if (quoteTimeSpan < timeSpan)
                {
                    timeSpan = timeSpan - quoteTimeSpan;
                }
                if (timeSpan > 15)
                {
                    count++;
                }
            }

            return count;
        }
        #endregion

        #region 详细信息
        public QWXZQDetailModel GetQWXZQDetailInfo(int year, int month)
        {
            QWXZQDetailModel model = new QWXZQDetailModel();

            model.BAOJIAZQ = GetBaoJiaZQ(year, month).ToString();
            model.BJSQZQ = GetBJSQZQ(year, month).ToString();
            model.BJWLZQ = GetBJWLZQ(year, month).ToString();
            model.FCZQ = GetFCZQ(year, month).ToString();
            model.FHZQ = GetFHZQ(year, month).ToString();
            model.PDZQ = GetPDZQ(year, month).ToString();
            model.SHZQ = GetSHZQ(year, month).ToString();
            model.SJSPZQ = GetSJSPZQ(year, month).ToString();
            model.SJWLZQ = GetSJWLZQ(year, month).ToString();
            model.WXZQ = GetWXZQ(year, month).ToString();

            return model;
        }

        public QWXZQStationModel GetQWXZQStationSummaryInfo(int stationID, int year, int month)
        {
            QWXZQStationModel model = new QWXZQStationModel();
            model.BAOJIACount = GetBaoJiaCount(stationID, year, month).ToString();
            model.BAOJIAZQ = GetBaoJiaZQ(stationID, year, month).ToString();
            model.BJSQCount = GetBJSQCount(stationID, year, month).ToString();
            model.BJSQZQ = GetBJSQZQ(stationID, year, month).ToString();
            model.BJWLZQ = GetBJWLZQ(stationID, year, month).ToString();
            model.CompleteCount = GetCompleteCount(stationID, year, month).ToString();
            model.FCCount = GetFCCount(stationID, year, month).ToString();
            model.FCZQ = GetFCZQ(stationID, year, month).ToString();
            model.FHZQ = GetFHZQ(stationID, year, month).ToString();
            model.L15 = GetL15CountOfMonth(stationID, year, month).ToString();
            model.L7To15 = Get7To15CountOfMonth(stationID, year, month).ToString();
            model.NotCompleteCount = GetNotCompleteCount(stationID, year, month).ToString();
            model.QZQ = GetQWXZQ(stationID, year, month).ToString();
            model.S7 = GetS7CountOfMonth(stationID, year, month).ToString();
            model.SHZQ = GetSHZQ(stationID, year, month).ToString();
            model.SJCount = GetSJCount(stationID, year, month).ToString();
            model.SJSPZQ = GetSJSPZQ(stationID, year, month).ToString();
            model.SJWLZQ = GetSJWLZQ(stationID, year, month).ToString();
            model.WXZQ = GetWXZQ(stationID, year, month).ToString();
            return model;
        }

        public List<QWXZQStationModel> GetQWXZQStationDetailInfo(int year, int month)
        {
            List<QWXZQStationModel> models = new List<QWXZQStationModel>();
            IQueryable<ServiceStation> stations = GetEntities<ServiceStation>();
            foreach (ServiceStation station in stations)
            {
                QWXZQStationModel model = new QWXZQStationModel();
                model.Station = station.Name;
                model.BAOJIACount = GetBaoJiaCount(station.ID, year, month).ToString();
                model.BAOJIAZQ = GetBaoJiaZQ(station.ID, year, month).ToString();
                model.BJSQCount = GetBJSQCount(station.ID, year, month).ToString();
                model.BJSQZQ = GetBJSQZQ(station.ID, year, month).ToString();
                model.BJWLZQ = GetBJWLZQ(station.ID, year, month).ToString();
                model.CompleteCount = GetCompleteCount(station.ID, year, month).ToString();
                model.FCCount = GetFCCount(station.ID, year, month).ToString();
                model.FCZQ = GetFCZQ(station.ID, year, month).ToString();
                model.FHZQ = GetFHZQ(station.ID, year, month).ToString();
                model.L15 = GetL15CountOfMonth(station.ID, year, month).ToString();
                model.L7To15 = Get7To15CountOfMonth(station.ID, year, month).ToString();
                model.NotCompleteCount = GetNotCompleteCount(station.ID, year, month).ToString();
                model.QZQ = GetQWXZQ(station.ID, year, month).ToString();
                model.S7 = GetS7CountOfMonth(station.ID, year, month).ToString();
                model.SHZQ = GetSHZQ(station.ID, year, month).ToString();
                model.SJCount = GetSJCount(station.ID, year, month).ToString();
                model.SJSPZQ = GetSJSPZQ(station.ID, year, month).ToString();
                model.SJWLZQ = GetSJWLZQ(station.ID, year, month).ToString();
                model.WXZQ = GetWXZQ(station.ID, year, month).ToString();
                models.Add(model);
            }

            return models;
        }

        public List<QWXYWPersonModel> GetQWXZQPersonDetailInfo(int year, int month)
        {
            List<QWXYWPersonModel> models = new List<QWXYWPersonModel>();

             IQueryable<User> users = GetEntities<User>().Where(u => u.UserType.ID != (int)RoleType.Customer && u.UserType.ID != (int)RoleType.InternalCustomer);
             foreach (User user in users)
             {
                 QWXYWPersonModel model = new QWXYWPersonModel();
                 model.Person = user.ContactPerson;
                 model.CompleteCount = GetCompleteCountOfPerson(user.UserID, year, month).ToString();
                 model.NotCompleteCount = GetNotCompleteCountOfPerson(user.UserID, year, month).ToString();
                 model.L15 = GetL15CountOfMonthByPerson(user.UserID, year, month).ToString();
                 model.L7To15 = Get7To15CountOfMonthByPerson(user.UserID, year, month).ToString();
                 model.QZQ = GetQWXZQOfPerson(user.UserID, year, month).ToString();
                 model.S7 = GetS7CountOfMonthByPerson(user.UserID, year, month).ToString();
                 models.Add(model);
             }

            return models;
        }

        public QWXYWPersonModel GetQWXZQPersonModel(long userID, int year, int month)
        {
            QWXYWPersonModel model = new QWXYWPersonModel();
            model.CompleteCount = GetCompleteCountOfPerson(userID, year, month).ToString();
            model.NotCompleteCount = GetNotCompleteCountOfPerson(userID, year, month).ToString();
            model.L15 = GetL15CountOfMonthByPerson(userID, year, month).ToString();
            model.L7To15 = Get7To15CountOfMonthByPerson(userID, year, month).ToString();
            model.QZQ = GetQWXZQOfPerson(userID, year, month).ToString();
            model.S7 = GetS7CountOfMonthByPerson(userID, year, month).ToString();
            return model;
        }

        public int GetBaoJiaCount(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.QuotationDateTime.HasValue &&
                p.ConfirmQuotationDateTime.HasValue && p.QuotationDateTime.Value > startDate && p.QuotationDateTime.Value < endDate &&
                p.ApplicationForm.ServiceStation.ID == stationID);
            return applications.Count();
        }

        public int GetBJSQCount(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);
            IQueryable<SparePartApplyForm> applications = GetEntities<SparePartApplyForm>().Where(s => s.ApplyTime.HasValue &&
                s.ApplyTime.Value > startDate && s.ApplyTime.Value < endDate && s.ServiceStation.ID == stationID);
            return applications.Count();
        }

        public int GetCompleteCount(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.RepairCompleteDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ServiceStation.ID == stationID);
            return applications.Count();
        }

        public int GetCompleteCountOfPerson(long userID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.RepairCompleteDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.User.UserID == userID);
            return applications.Count();
        }

        public int GetNotCompleteCount(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => !p.RepairCompleteDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ServiceStation.ID == stationID);
            return applications.Count();
        }

        public int GetNotCompleteCountOfPerson(long userID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => !p.RepairCompleteDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.User.UserID == userID);
            return applications.Count();
        }

        public int GetFCCount(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);
            IQueryable<ReturnToFactoryForm> applications = GetEntities<ReturnToFactoryForm>().Where(r => r.ApplyDate.HasValue &&
               r.ApplyDate.Value > startDate && r.ApplyDate.Value < endDate && r.ApplicationProduct.ApplicationForm.ServiceStation.ID == stationID);
            return applications.Count();
        }

        public int GetSJCount(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.SendBackDateTime.HasValue &&
               p.ApplicationForm.SendBackDateTime.Value > startDate && p.ApplicationForm.SendBackDateTime.Value < endDate &&
               p.ApplicationForm.ServiceStation.ID == stationID);
            return applications.Count();
        }

        public int GetPDZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p=>p.DistributeDateTime.HasValue && p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate);

            int totalSpan = 0;           
                
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.DistributeDateTime.Value);
                totalSpan += timeSpan;
            }

            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetWXZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => (p.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) && p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate && p.RepairCompleteDateTime.HasValue);

            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);
                //if (application.ApplicationForm.NortificationDatetime.HasValue)
                //{
                //    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                //}
                //else if (application.ClientReceiveDateTime.HasValue)
                //{
                //    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ClientReceiveDateTime.Value);
                //}

                //if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                //{
                //    int baoJiaZQ = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                //    if (baoJiaZQ > 0 && baoJiaZQ < timeSpan)
                //    {
                //        timeSpan -= baoJiaZQ;
                //    }
                //}

                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetWXZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => (p.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) && p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.RepairCompleteDateTime.HasValue &&
                p.ApplicationForm.ServiceStation.ID == stationID);

            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);

                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }


        public int GetQWXZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => (p.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue)  && p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate);

            int totalSpan = 0;  
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                int timeSpan = 0;
                if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }
                else if (application.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ClientReceiveDateTime.Value);
                }
                
                if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    int baoJiaZQ = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    if (baoJiaZQ > 0 && baoJiaZQ < timeSpan)
                    {
                        timeSpan -= baoJiaZQ;
                    }
                }
                 
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetQWXZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => (p.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) && p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.ApplicationForm.ServiceStation.ID == stationID);

            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                int timeSpan = 0;
                if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }
                else if (application.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ClientReceiveDateTime.Value);
                }

                if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    int baoJiaZQ = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    if (baoJiaZQ > 0 && baoJiaZQ < timeSpan)
                    {
                        timeSpan -= baoJiaZQ;
                    }
                }

                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetQWXZQOfPerson(long userID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => (p.ClientReceiveDateTime.HasValue || p.ApplicationForm.NortificationDatetime.HasValue) && p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && p.ApplicationForm.ReceiveDateTime.Value < endDate &&
                p.User.UserID == userID);

            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                int timeSpan = 0;
                if (application.ApplicationForm.NortificationDatetime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ApplicationForm.NortificationDatetime.Value);
                }
                else if (application.ClientReceiveDateTime.HasValue)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.ReceiveDateTime.Value, application.ClientReceiveDateTime.Value);
                }

                if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                {
                    int baoJiaZQ = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    if (baoJiaZQ > 0 && baoJiaZQ < timeSpan)
                    {
                        timeSpan -= baoJiaZQ;
                    }
                }

                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetSJSPZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<UpgradeForm> applications = GetEntities<UpgradeForm>().Where(u => u.ApplyTime.HasValue && u.DealWithTime.HasValue &&
                u.ApplyTime.Value > startDate && u.ApplyTime.Value < endDate);
            int totalSpan = 0;
            foreach (UpgradeForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplyTime.Value, application.DealWithTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetSJSPZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<UpgradeForm> applications = GetEntities<UpgradeForm>().Where(u => u.ApplyTime.HasValue && u.DealWithTime.HasValue &&
                u.ApplyTime.Value > startDate && u.ApplyTime.Value < endDate &&
                u.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (UpgradeForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplyTime.Value, application.DealWithTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetBJSQZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<SparePartApplyForm> applications = GetEntities<SparePartApplyForm>().Where(s => s.ApplyTime.HasValue && s.DealWithTime.HasValue &&
                s.ApplyTime.Value > startDate && s.ApplyTime.Value < endDate);
            int totalSpan = 0;
            foreach(SparePartApplyForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplyTime.Value, application.DealWithTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetBJSQZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<SparePartApplyForm> applications = GetEntities<SparePartApplyForm>().Where(s => s.ApplyTime.HasValue && s.DealWithTime.HasValue &&
                s.ApplyTime.Value > startDate && s.ApplyTime.Value < endDate && s.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (SparePartApplyForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplyTime.Value, application.DealWithTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetBaoJiaZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.QuotationDateTime.HasValue &&
                p.ConfirmQuotationDateTime.HasValue && p.ApplicationForm.Gathering != null && p.ApplicationForm.Gathering.PayDateTime.HasValue &&
                p.QuotationDateTime.Value > startDate && p.QuotationDateTime.Value < endDate);
            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                if (!application.ApplicationForm.GatheringReference.IsLoaded)
                {
                    application.ApplicationForm.GatheringReference.Load();
                }

                if (application.ApplicationForm.Gathering != null)
                {
                    int timeSpan = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ApplicationForm.Gathering.PayDateTime.Value);
                    totalSpan += timeSpan;
                }
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetBaoJiaZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.QuotationDateTime.HasValue &&
                p.ConfirmQuotationDateTime.HasValue && p.ApplicationForm.Gathering != null && p.ApplicationForm.Gathering.PayDateTime.HasValue &&
                p.QuotationDateTime.Value > startDate && p.QuotationDateTime.Value < endDate &&
                p.ApplicationForm.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                if (!application.ApplicationForm.GatheringReference.IsLoaded)
                {
                    application.ApplicationForm.GatheringReference.Load();
                }

                if (application.ApplicationForm.Gathering != null)
                {
                    int timeSpan = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ApplicationForm.Gathering.PayDateTime.Value);
                    totalSpan += timeSpan;
                }
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetFCZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ReturnToFactoryForm> applications = GetEntities<ReturnToFactoryForm>().Where(r => r.ApplyDate.HasValue && r.ReceiveDate.HasValue &&
                r.ApplyDate.Value > startDate && r.ApplyDate.Value < endDate);
            int totalSpan = 0;
            foreach (ReturnToFactoryForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplyDate.Value, application.ReceiveDate.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetFCZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ReturnToFactoryForm> applications = GetEntities<ReturnToFactoryForm>().Where(r => r.ApplyDate.HasValue && r.ReceiveDate.HasValue &&
                r.ApplyDate.Value > startDate && r.ApplyDate.Value < endDate && r.ApplicationProduct.ApplicationForm.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (ReturnToFactoryForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplyDate.Value, application.ReceiveDate.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetSJWLZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<UpgradeForm> applications = GetEntities<UpgradeForm>().Where(u => u.SendTime.HasValue && u.ReceiveTime.HasValue &&
                u.SendTime.Value > startDate && u.SendTime.Value < endDate);
            int totalSpan = 0;
            foreach (UpgradeForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.SendTime.Value, application.ReceiveTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetSJWLZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<UpgradeForm> applications = GetEntities<UpgradeForm>().Where(u => u.SendTime.HasValue && u.ReceiveTime.HasValue &&
                u.SendTime.Value > startDate && u.SendTime.Value < endDate &&
                u.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (UpgradeForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.SendTime.Value, application.ReceiveTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetBJWLZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<SparePartApplyForm> applications = GetEntities<SparePartApplyForm>().Where(s => s.SendTime.HasValue && s.ReceiveTime.HasValue &&
                s.SendTime.Value > startDate && s.SendTime.Value < endDate);
            int totalSpan = 0;
            foreach (SparePartApplyForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.SendTime.Value, application.ReceiveTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetBJWLZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<SparePartApplyForm> applications = GetEntities<SparePartApplyForm>().Where(s => s.SendTime.HasValue && s.ReceiveTime.HasValue &&
                s.SendTime.Value > startDate && s.SendTime.Value < endDate && s.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (SparePartApplyForm application in applications)
            {
                int timeSpan = DateTimeService.CalculateDateTimeSpan(application.SendTime.Value, application.ReceiveTime.Value);
                totalSpan += timeSpan;
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetSHZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.SendBackDateTime.HasValue &&
               p.ApplicationForm.ClientReceiveDateTime.HasValue && 
               p.ApplicationForm.SendBackDateTime.Value > startDate &&
               p.ApplicationForm.SendBackDateTime.Value < endDate);
            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                if (!application.ApplicationForm.GatheringReference.IsLoaded)
                {
                    application.ApplicationForm.GatheringReference.Load();
                }

                if (application.ApplicationForm.Gathering != null)
                {
                    int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.SendBackDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                    totalSpan += timeSpan;
                }
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetSHZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.SendBackDateTime.HasValue &&
               p.ApplicationForm.ClientReceiveDateTime.HasValue &&
               p.ApplicationForm.SendBackDateTime.Value > startDate &&
               p.ApplicationForm.SendBackDateTime.Value < endDate &&
               p.ApplicationForm.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                if (!application.ApplicationForm.GatheringReference.IsLoaded)
                {
                    application.ApplicationForm.GatheringReference.Load();
                }

                if (application.ApplicationForm.Gathering != null)
                {
                    int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.SendBackDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                    totalSpan += timeSpan;
                }
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetFHZQ(int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.SendBackDateTime.HasValue &&
               p.ApplicationForm.ClientReceiveDateTime.HasValue && 
               p.RepairCompleteDateTime.Value > startDate &&
               p.RepairCompleteDateTime.Value < endDate);
            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                if (!application.ApplicationForm.GatheringReference.IsLoaded)
                {
                    application.ApplicationForm.GatheringReference.Load();
                }

                if (application.ApplicationForm.Gathering != null)
                {
                    int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.SendBackDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                    totalSpan += timeSpan;
                }
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }

        public int GetFHZQ(int stationID, int year, int month)
        {
            DateTime startDate = DateTime.Now;
            if (month == 1)
            {
                startDate = new DateTime(year - 1, 12, 26);
            }
            else
            {
                startDate = new DateTime(year, month - 1, 26);
            }
            DateTime endDate = new DateTime(year, month, 25);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.SendBackDateTime.HasValue &&
               p.ApplicationForm.ClientReceiveDateTime.HasValue &&
               p.RepairCompleteDateTime.Value > startDate &&
               p.RepairCompleteDateTime.Value < endDate &&
               p.ApplicationForm.ServiceStation.ID == stationID);
            int totalSpan = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }
                if (!application.ApplicationForm.GatheringReference.IsLoaded)
                {
                    application.ApplicationForm.GatheringReference.Load();
                }

                if (application.ApplicationForm.Gathering != null)
                {
                    int timeSpan = DateTimeService.CalculateDateTimeSpan(application.ApplicationForm.SendBackDateTime.Value, application.ApplicationForm.ClientReceiveDateTime.Value);
                    totalSpan += timeSpan;
                }
            }
            if (totalSpan > 0)
            {
                return totalSpan / applications.Count();
            }
            else
            {
                return 0;
            }
        }
        #endregion
    }
}
